﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HealthDietAdviceForm
 * 类型：       控制层
 * 功能说明：   饮食健康画面
 * 作者：       王剑华
 * 创建日期：   2007/09/11
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.System.Db;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Inp;
using Shpm.System.Web.Control;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Utils;
using log4net;
using Shpm.System.Validator;

public partial class App_Input_HealthDiseaseForm : System.Web.UI.Page
{
    HealthDiseaseDao dao = new HealthDiseaseDao();

    public const int DEFAULTROW_WORKEXP = 0;

    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HealthDiseaseForm));

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 取得当前登录的医生id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyJob = mstSysUserBean == null ? "" : mstEmpBean.EmployeeJob;
            string emplyLevel = mstSysUserBean == null ? "" : mstEmpBean.EmployeeLevel;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            if ((string)Request.QueryString["MEMBER_NO"] == null)
            {
                ViewState["MEMBER_NO"] = "";
            }
            else
            {
                ViewState["MEMBER_NO"] = (string)Request.QueryString["MEMBER_NO"];
            }
            ViewState["DOCTOR_ID"] = emplyId;


            HealthDiseaseBean paramBean = new HealthDiseaseBean();
            paramBean.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);
            // edit
            paramBean = dao.Load(paramBean);
            if (paramBean != null)
            {
                LblDoctorId.Text = paramBean.DoctorId;
                LblMemberNo.Text = paramBean.MemberNo;
                //LblMemberNo.Text = Convert.ToString(ViewState["MEMBER_NO"]);
                LblMemberName.Text = paramBean.NameC;
                LblSex.Text = paramBean.Sex;
                LblTel.Text = paramBean.Tel;
                LblApartmentNo.Text = paramBean.ApartmentNo;
            }

            setDisease();
            InitDisease();

        }       
    }

    private void setDisease()
    {
        HealthDiseaseDao dao = new HealthDiseaseDao();
        CommonDictionaryBean bean = new CommonDictionaryBean();

        bean.Catalog = "DISEASE_NAME";
        IList list = dao.FindByCatalog(bean);
        list.Insert(0,new CommonDictionaryBean());
        this.DiseaseName.DataSource = list;
        DiseaseName.DataBind();

    }

    private void InitDisease()
    {
        IList lstRst = null;
        HealthDiseaseBean beanHT = new HealthDiseaseBean();
        beanHT.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);
        lstRst = dao.LoadDiseaseType(beanHT);
        this.GridView1.DataSource = lstRst;
        this.GridView1.DataBind();
    }
    //更新
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if(ComValidator1.DoValidator() == false )
            {
                ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
                    return;
            }
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;


           
            HealthDiseaseBean bean = new HealthDiseaseBean();
            bean.DiseaseName = this.DiseaseName.SelectedValue;
            bean.MemberNo = LblMemberNo.Text;
            bean.UserId = loginId;
            if (!ViewState.IsItemDirty("ROWID"))
            {
              
                dao.Insert(bean);
                ((Design_MasterPage)this.Master).InfoMessage =
          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
            else 
            {
                bean.RowId = Convert.ToInt32(ViewState["ROWID"]);
                dao.Update(bean);
                ((Design_MasterPage)this.Master).InfoMessage =
          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            }

            ViewState.Remove("ROWID");
            this.DiseaseName.SelectedValue = "";
            //this.txtDiseaseName.Text = "";
            InitDisease();
          
        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
            throw ex;
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        GridView1.PageIndex = e.NewPageIndex;
        InitDisease();
        //GridView1.DataSource = InitMemberSrh();
        //GridView1.DataBind();

    }
    protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        int row = e.NewEditIndex;
        ViewState["ROWID"] = (sender as GridView).DataKeys[row].Value.ToString();
        string diseaseId = null;
        int index = Convert.ToInt32(e.NewEditIndex);
        DataKey key = GridView1.DataKeys[index];
        diseaseId = Convert.ToString(key.Value);

        HealthDiseaseBean bean = new HealthDiseaseBean();
        HealthDiseaseDao dao = new HealthDiseaseDao();
        bean.RowId = Convert.ToInt32(diseaseId);
        bean = dao.findDiseaseInfo(bean);
        this.DiseaseName.SelectedValue = Convert.ToString(bean.DiseaseName);
        //this.txtDiseaseName.Text = (sender as GridView).Rows[row].Cells[0].Text;
    }
    protected void GridView1_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string  rowId = (sender as GridView).DataKeys[e.RowIndex].Value.ToString();
        HealthDiseaseBean bean = new HealthDiseaseBean();
        bean.RowId = Convert.ToInt32(rowId);
        dao.Delete(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();


        ViewState.Remove("ROWID");
        this.DiseaseName.SelectedValue = "";
        //this.txtDiseaseName.Text = "";
        InitDisease();
    }

    protected void btn_new_Click(object sender, EventArgs e)
    {
        ViewState.Remove("ROWID");
        this.DiseaseName.SelectedValue = "";
        //this.txtDiseaseName.Text = "";
    }
}
